<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class User extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $userList = model('user')->getList();
        //$userList = Db::name('user')->field('id,username,brand,team,status')->select();
        //p($userList);die;
        $this->assign('userList',$userList);
        return view();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $groupTitle = db('authGroup')->field('id,title')->where('status','<>',0)->select();
        $brand = db('brand')->select();
        $this->assign('groupTitle',$groupTitle);
        $this->assign('brand',$brand);
        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        if (!request()->isPost()) {
            $this->error('非法请求！');
        }
        $data = $request->post();
        $data['password'] = encrypt($data['password']);
        $res = model('user')->save($data);
        $uid = model('user')->id;
        $_data=array();//对应管理员和用户组
        $_data['uid']=$uid;
        $_data['group_id']=$data['group_id'];
        $addGroupAccess=db('authGroupAccess')->insert($_data);

        if ($res && $addGroupAccess) {
            $this->success('添加成功', url('index'));
        } else {
            $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $user=db('user')->field('id,username,brand_id,group_id,status')->find($id);
        if (empty($user)) {
            $this->error('用户不存在');
        }
        $group = db('authGroup')->field('id,title')->where('status','<>',0)->select();
        $brand = db('brand')->field('id,brand')->select();

        $this->assign([
            'user'=>$user,
            'group'=>$group,
            'brand'=>$brand,
        ]);
        return view();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if (!request()->isPost()) {
            $this->error('非法请求！');
        }
        $data = $request->post();
        $data['password'] = encrypt($data['password']);
        $res = model('user')->save($data,['id' => $id]);
        //$uid = model('user')->id;
        $group_id = $data['group_id'];
        db('authGroupAccess')->where('uid',$id)->update(['group_id' => $group_id]);
        //p($addGroupAccess);die;
        if ($res) {
            $this->success('修改成功', url('admin/user/index'));
        } else {
            $this->error('修改失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if($id == 1){
            $this->error('超级管理员不可删除');
        }
        $res = model('user')->deleteById($id);
        if($res == 1){
            $this->success('删除成功', url('index'));
        }else{
            $this->error('删除失败');
        }
    }

    public function changeStatus($id,$status){
        if($id == 1){
            $this->error('超级管理员不可禁用');
        }
        $res = model('user')->save(['status' => $status],['id' => $id]);
        if($res == 1){
            $this->success('状态修改成功', url('index'));
        }else{
            $this->error('状态修改失败');
        }
    }
}
